package com.hbhmzy.contact;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hbhmzy.call.R;


public class ContactEditActivity extends Activity implements OnClickListener{

	private EditText etName;
	private EditText etNumber;
	private String name;
	private String number;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.contact_edit);
		// 默认键盘弹出
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
		init();
		//设置状态栏颜色
//		Sofia.with(this).statusBarBackground(getResources().getColor(R.color.GREEN_ROOT));
	}
	
	private void init() {
		Button cancel = findViewById(R.id.root_cancel);
		cancel.setOnClickListener(this);

		TextView title = findViewById(R.id.root_title3);
		title.setText("编辑联系人");

		Button finish = findViewById(R.id.root_ok);
		finish.setOnClickListener(this);

		LinearLayout delete = findViewById(R.id.contact_delete);
		delete.setOnClickListener(this);

		etName = findViewById(R.id.editContact_name);
		etNumber = findViewById(R.id.editContact_number);

		// 取出从上一个 activity 中传递过来的值，设置待姓名和号码编辑框
		Intent intent = getIntent();
		name = intent.getStringExtra("name");
		number = intent.getStringExtra("number");
		etName.setText(name);
		etNumber.setText(number);	
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.root_cancel:   // 取消编辑			
			finish();
			break;

		case R.id.root_ok:   // 编辑成功
			String newName = etName.getText().toString();
			String newNumber = etNumber.getText().toString();
			// 若编辑后的内容无变化,则直接返回
			if ((newName.equals(name)) && (newNumber.equals(number))) {				
				finish();
			}else if("".equals(newName) && "".equals(newNumber)){
				Toast.makeText(this, "您有信息未填写", Toast.LENGTH_SHORT).show();
			}else {	//否则根据编辑后的信息修改数据库对应的内容,再返回具体信息页面
				if("".equals(newName) && !"".equals(newNumber)){
					newName=newNumber;
				}

				String newSortkey = ContactsActivity.getSortKey(newName);
				ContactDBHelper dbHelper = new ContactDBHelper(ContactEditActivity.this, "mycontact.db3", null, 1);
				ContactDBManager.update(dbHelper.getReadableDatabase(), name, number, newName, newNumber, newSortkey);
				Toast.makeText(ContactEditActivity.this, "编辑成功！", Toast.LENGTH_SHORT).show();

				Intent intent2 = new Intent();
                intent2.putExtra("name", newName);
                intent2.putExtra("number", newNumber);
				setResult(Constant.EDIT_RESULT_CODE, intent2);
				finish();
			}
			break;
			
		case R.id.contact_delete: // 删除
            AlertDialog dialog = new AlertDialog.Builder(this)
                    .setIcon(R.mipmap.ic_delete)//设置标题的图片
                    .setTitle("温馨提示")//设置对话框的标题
                    .setMessage("确定删除该联系人吗？")//设置对话框的内容
                    //设置对话框的按钮
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    })
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();

                            ContactDBHelper dbHelper = new ContactDBHelper(ContactEditActivity.this, "mycontact.db3", null, 1);
                            ContactDBManager.deleteByNameNum(dbHelper.getReadableDatabase(), name, number);
                            Toast.makeText(ContactEditActivity.this, "已删除联系人: " + name + " " + number, Toast.LENGTH_SHORT)
                                    .show();

                            setResult(Constant.DELETE_RESULT_CODE);
                            finish();
                        }
                    }).create();
            dialog.show();
			break;
		}
		
	}
}
